package com.twu.biblioteca;

import com.twu.biblioteca.auth.Authenticator;
import com.twu.biblioteca.auth.User;
import com.twu.biblioteca.domain.Book;
import com.twu.biblioteca.domain.Movie;
import com.twu.biblioteca.domain.Repository;
import com.twu.biblioteca.domain.RepositoryFactory;
import com.twu.biblioteca.io.ConsoleIO;
import com.twu.biblioteca.io.IO;
import com.twu.biblioteca.ui.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    public static void main(String[] args) {
        setup();

        System.out.println(Welcomer.getMessage());

        String username = io.promptForInput("Enter username:");
        String password = io.promptForInput("Enter password:");
        authenticator.login(username, password);

        if (!authenticator.isLoggedIn()) {
            System.out.println("You are not logged in.\n");
        } else {
            System.out.println("Login successful\n");
        }

        System.out.println(menu);
        Scanner scanner = new Scanner(System.in);

        while (true) {
            try {
                String selectedItem = scanner.next();
                menu.select(selectedItem);

                System.out.println("\n" + menu);
            } catch (InvalidMenuSelectionException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    private static IO io;
    private static Menu menu;
    private static Repository<Book> bookRepository;
    private static Repository<Movie> movieRepository;
    private static Authenticator authenticator;

    private static void setup() {
        io = new ConsoleIO();
        bookRepository = RepositoryFactory.createDummyBookRepository();
        movieRepository = RepositoryFactory.createDummyMovieRepository();
        authenticator = createAuthenticator();
        menu = createMenu();
    }

    private static Authenticator createAuthenticator() {
        // user list is pre-defined
        // so there is no need to make Authenticator ensure that the username is in the right format yet
        List<User> users = new ArrayList<User>();

        User user = new User("111-1111", "pwd", "user1@mail.com", "88882222");
        users.add(user);

        user = new User("111-1112", "pwd2", "anotheruser@xyzcorp.com", "1243333");
        users.add(user);

        Authenticator authenticator = new Authenticator(users);
        return authenticator;
    }

    private static Menu createMenu() {
        Menu menu = new Menu(authenticator);

        menu.addItem(new MenuItem("vb", "View list of all books in the library", new ViewBookListMenuItemHandler(bookRepository, io), false));
        menu.addItem(new MenuItem("vm", "View list of all movies in the library", new ViewMovieListMenuItemHandler(movieRepository, io), false));
        menu.addItem(new MenuItem("r", "Reserve a book", new ReserveBookMenuItemHandler(bookRepository, io), true));
        menu.addItem(new MenuItem("c", "Check membership details", new CheckMembershipDetailsMenuItemHandler(authenticator, io), false));
        menu.addItem(new MenuItem("q", "Close Biblioteca", new CloseMenuItemHandler(), false));

        return menu;
    }
}
